#include <assert.h>
#include <stdlib.h>
#include "link.h"

#define SENTINELLE NULL

struct link lnk__empty ()
{
  struct link lnk = {SENTINELLE};
  return lnk;
}

int lnk__is_end ( const struct link * l, const struct lelement * e)
{
  return e==SENTINELLE;
}

int lnk__add_head( struct link * lnk, struct lelement * elem)
{
  elem->next = lnk->head;

	lnk->head = elem;
  return 0;
}

int lnk__remove_head( struct link* lnk )
{
	if(lnk->head == SENTINELLE)
		return 1;
	lnk->head = lnk->head->next;
	return 0;
}

int lnk__add_tail( struct link * l, struct lelement * e)
{
  assert(l!=NULL);

  struct lelement* e_next = l->head;
  while(e_next->next != SENTINELLE)
    e_next = e_next->next;

  e_next->next = e;
  e->next = 0;

  return 0;
}

int lnk__remove_after(struct link* lnk, struct lelement* e_index)
{
  if(e_index == NULL)
  {
    lnk__remove_head(lnk);
    return 0;
  }
  if(e_index->next == SENTINELLE)
    return 0;

  e_index->next = e_index->next->next;
  return 0;
}
